import axios, { AxiosError } from "axios";
import { useEffect, useState } from "react";
type ApiResponse<T> = {
  data: T | null;
  loading: boolean;
  error: AxiosError | null;
};

const useFetch = <T>(url: string): ApiResponse<T> => {
  console.log(url, "x");
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState(null);
  const [data, setData] = useState<T | null>(null);

  useEffect(() => {
    const fetchData = async () => {
      try {
        const response = await axios.get(url);
        setData(response.data);
      } catch (error: any) {
        setError(error);
      } finally {
        setLoading(false);
      }
    };

    fetchData();

    return () => {};
  }, [url]);

  return { data, loading, error };
};

export default useFetch;
